﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;
using MM.Common;

namespace MobileManager
{
    public partial class MODEPManage : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("Login.aspx");
                    return;
                }
                else
                {
                    userModel = (MM.Model.T_Users)Session["CurUser"];
                    if (userModel.RoleID.Equals("3"))
                        return;
                }
                if (!IsPostBack)
                {
                    GVDataBindAll();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strNO = txtNO.Text.Trim();
                if (string.IsNullOrEmpty(strNO))
                {
                    Common.PageStartAlert(this.Page, "运营商编号不能为空！");
                    return;
                }
                if (BLSqlData.ExistODEPNO(strNO))
                {
                    Common.PageStartAlert(this.Page, "运营商编号已经存在，请更换！");
                    return;
                }
                string strName = txtName.Text.Trim();
                if (string.IsNullOrEmpty(strName))
                {
                    Common.PageStartAlert(this.Page, "运营商名称不能为空！");
                    return;
                }
                string strSQL = string.Format("INSERT INTO T_ODEP(ONO,OName) VALUES('{0}',N'{1}')", strNO, strName);
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("添加了运行商信息：运行商编号={0}。", strNO), "运行商管理");
                Common.PageStartAlert(this.Page, "添加成功！");
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region GV info
        private void GVDataBind()
        {
            string strSQL = "SELECT a.ONO,a.OName FROM T_ODEP a WITH(NOLOCK) WHERE 1=1";
            if (!string.IsNullOrEmpty(txtNO.Text.Trim()))
                strSQL += string.Format(" AND a.ONO LIKE '%{0}%'", txtNO.Text.Trim());
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
                strSQL += string.Format(" AND a.OName LIKE N'%{0}%'", txtName.Text.Trim());
            strSQL += " ORDER BY a.CreateTime DESC";

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        private void GVDataBindAll()
        {
            DataSet ds = DbHelperSql.Query("SELECT a.ONO,a.OName FROM T_ODEP a WITH(NOLOCK) ORDER BY a.CreateTime DESC");
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strNO = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                ArrayList alist = new ArrayList();
                alist.Add(string.Format("DELETE FROM T_ODEP WHERE ONO='{0}'", strNO));
                alist.Add(string.Format("DELETE FROM T_Rules WHERE ONO='{0}'", strNO));
                DbHelperSql.ExecuteSqlTran(alist);

                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("删除了运行商信息：运行商编号={0}，同时删除了相应规则信息。", strNO), "运行商管理");
                Common.PageStartAlert(this.Page, "删除成功！");
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                //运营商编号添加后不给修改，modified by leo 2011-03-20
                //string strNO = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtNO")).Text.Trim();
                //if (string.IsNullOrEmpty(strNO))
                //{
                //    MM.Common.Common.PageStartAlert(this.Page, "运行商编号不能为空！");
                //    return;
                //}
                //string strOldNO = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;
                //if (!strOldNO.Equals(strNO))
                //{
                //    if (Common.ExistODEPNO(strNO))
                //    {
                //        MM.Common.Common.PageStartAlert(this.Page, "运行商编号已经存在，请更换！");
                //        return;
                //    }
                //}
                string strNO = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;

                string strName = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
                if (string.IsNullOrEmpty(strName))
                {
                    Common.PageStartAlert(this.Page, "运营商名称不能为空！");
                    return;
                }

                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_ODEP SET OName=N'{0}' WHERE ONO='{1}'", strName, strNO));
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("更新了运营商信息：运营商编号={0}。", strNO), "运营商管理");
                Common.PageStartAlert(this.Page, "更新成功！");

                GVinfo.EditIndex = -1;
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion
    }
}
